package harvard.cscie160.hw7.project;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;

/**
 * 
 * @author M Bret Blackford
 *
 */
public class BankImpl extends UnicastRemoteObject implements BankInterface {
	
	private HashMap<Integer, AccountInterface> accounts;
	private static BankImpl bankInstance = null;

	public BankImpl() throws RemoteException {
		
		accounts = new HashMap<Integer, AccountInterface>();
		
		//default account balances per requirements
		float[] defaultBalances = { 0, 100, 500 };
		
		for (int i = 0; i < 3; i++) {
			AccountImpl account = new AccountImpl(i + 1);
			account.setAccountBalance(defaultBalances[i]);
			accounts.put(i + 1, account);
		}
		SecurityImpl security = SecurityImpl.getSecurityInstance();
		security.setSecurityAccess(1, "1234", true, true, true);
		security.setSecurityAccess(2, "2345", true, false, true);
		security.setSecurityAccess(3, "3456", false, true, true);
	}


	public AccountInterface getAccount(int accountNumber) throws RemoteException {
		
		AccountInterface account = accounts.get(accountNumber);
		
		if (account == null) {
			throw new ATMException("ERROR in BankImpl.getAccount() - unknown accountNumber " + account);
		}
		
		return account;
	}


	public SecurityInterface getSecurity() throws RemoteException {
		return SecurityImpl.getSecurityInstance();
	}

	public void resetDeafaults() throws RemoteException {

		System.out.println(DateUtils.now() + " in BankImpl.resetDefaults()");
		
		for (AccountInterface account : accounts.values()) {
			
			int accountNumber = account.getAccountIdNumber();

			if (accountNumber == 1) {
				account.setAccountBalance(0);
			} else if (accountNumber == 2) {
				account.setAccountBalance(100);
			} else if (accountNumber == 3) {
				account.setAccountBalance(500);
			} else {
				String msg = DateUtils.now() + " ERROR: unknown default account :" + accountNumber;
				System.out.println(msg);
				throw new RemoteException(msg);
			}
		}
	}

}

